package com.yss.activemq_demo.config;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.jms.core.JmsTemplate;

import javax.jms.ConnectionFactory;
import javax.jms.Topic;

/**
 * ActiveMQ的配置类
 *
 * @author guoqiangfeng
 * @since 2018/6/15
 */
@Configuration
public class ActiveMqConfig {

    @Value("${activeMq.acs.topicName}")
    private String topic;

    @Value("${spring.activemq.broker-url}")
    private String brokeUrl;

    @Value("${spring.activemq.user}")
    private String username;

    @Value("${spring.activemq.password}")
    private String password;

    /**
     * 发布订阅模式
     * @return
     */
    @Bean
    public Topic topic() {

        return new ActiveMQTopic(topic);
    }

    @Bean
    public ConnectionFactory connectionFactory(){
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
        connectionFactory.setBrokerURL(brokeUrl);
        connectionFactory.setUserName(username);
        connectionFactory.setPassword(password);
        return connectionFactory;
    }

    @Bean("jmsTemplate")
    public JmsTemplate genJmsTemplate(){
        return new JmsTemplate(connectionFactory());

    }

    @Bean("jmsMessagingTemplate")
    public JmsMessagingTemplate jmsMessageTemplate(){
        return new JmsMessagingTemplate(connectionFactory());
    }

}
